
export default class Hero {
  name = ''
  hp = 0 // 血量
  minAp = 0
  maxAp = 0

  constructor(className, name, hp, minAp, maxAp,
      strikeRate=0, dodgeRate=0
    ) {
    this.className = className
    this.name = name
    this.hp = hp
    this.minAp = minAp
    this.maxAp = maxAp
    this.strikeRate = strikeRate // 暴击率 0-1 之间小数
    this.dodgeRate = dodgeRate //  闪避率 0-1 之间小数
  }
  get death() {
    return this.hp<=0
  }
  getAp() {
    return Math.floor(
      this.minAp
      + Math.random()*(this.maxAp-this.minAp)
    )
  }
  toString() {
    return `${this.name}还有${this.hp}点hp`
  }
  attack(hero) {
    if(!(hero instanceof Hero)) { return }
    let ap = this.getAp()
    let strike = Math.random() < this.strikeRate // 是否暴击
    let dodge = Math.random() < hero.dodgeRate // 是否闪避
    ap = ap * (strike ? 2 : 1)
    if(!dodge) {
      hero.hp = hero.hp - ap
    }
    return Hero.log(this, hero, ap, strike, dodge)
  }
  static log(s, o, ap, strike, dodge) {
    return `
      <div class='log
          ${s.className}
          ${strike ? 'strike':''}
          ${dodge ? 'dodge':''}
          '>
        <span class='hero'>${s.name}</span> 进攻
        <span class='hero'>${o.name}</span>
        ${
          strike ?
            `大力出奇迹！对他造成双倍伤害
              <span class='dm'>${ap}</span> 点` :
            `对他造成 <span class='dm'>${ap}</span> 点伤害`
        }
        ，
        ${dodge ? `但是被闪开了...` : ''}
        <span class='hero'>${o.name}</span> 还有
        <span class='hp ${o.hp <=0 ? 'death': ''}'>
          ${o.hp}
        </span>
        点生命值
      </div>`
  }
  static deathMsg(h) {
    return `
      <div class='log ${h.className} lost'>
        <span class='hero'>${h.name}</span> 输了。
      </div>
    `
  }
}